﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///收货地址簿
    /// </summary>
    public partial class user_addr_book
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public user_addr_book()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_user_addr_book>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_user_addr_book model)
        {
            return data.Insert(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_user_addr_book model)
        {
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return data.Delete<Entitys.dt_user_addr_book>(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_user_addr_book GetModel(int id)
        {
            return data.Get<Entitys.dt_user_addr_book>(id);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Entitys.dt_user_addr_book> GetList(string strWhere)
        {
            return data.List<Entitys.dt_user_addr_book>(strWhere);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_user_addr_book> GetList(int Top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_user_addr_book>(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_user_addr_book> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return data.List<Entitys.dt_user_addr_book>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id, string user_name)
        {
            return data.Exists<Entitys.dt_user_addr_book>(x => x.id == id && x.user_name == user_name);
        }

        /// <summary>
        /// 根据用户名删除一条数据
        /// </summary>
        public bool Delete(int id, string user_name)
        {
            return data.Delete<Entitys.dt_user_addr_book>(x => x.id == id && x.user_name == user_name)>0;
        }

        /// <summary>
        /// 返回默认的地址
        /// </summary>
        public Entitys.dt_user_addr_book GetDefault(string user_name)
        {
            return data.Get<Entitys.dt_user_addr_book>(x => x.user_name == user_name && x.is_default == 1);
        }

        /// <summary>
        /// 设置为默认的收货地址
        /// </summary>
        public void SetDefault(int id, string user_name)
        {
            List<Entitys.dt_user_addr_book> list = data.List<Entitys.dt_user_addr_book>(x => x.user_name == user_name);
            foreach (Entitys.dt_user_addr_book model in list)
            {
                if (id == model.id)
                {
                    model.is_default = 1;
                }
                else
                {
                    model.is_default = 0;
                }
                data.Update(model);
            }
        }
        #endregion
    }
}